package com.crm.system.controller.taskManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.taskManager.TaskInfo;
import com.crm.model.qo.taskManager.TaskUsersQO;
import com.crm.model.qo.taskManager.TaskInfoQO;
import com.crm.model.qo.taskManager.TaskUserQO;
import com.crm.model.vo.taskManager.TaskInfoVO;
import com.crm.model.vo.taskManager.TaskUserVO;
import com.crm.model.vo.taskManager.TodayTaskVO;
import com.crm.system.service.taskManager.TaskInfoService;
import com.crm.system.service.taskManager.TaskUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * 任务管理
 * @Author renshaorong
 * @Date 2021/4/25
 */
@RestController
@RequestMapping("/task")
@Slf4j
public class TaskManagerController {

    @Autowired
    private TaskInfoService taskInfoService;

    @Autowired
    private TaskUserService taskUserService;

    /**
     * 任务信息分页数据
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param taskInfoQO
     * @return
     */
    @PostMapping("taskInfo/getAllTaskInfo")
    public DataResponse getAllTaskInfo(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                       @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                       @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                       @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                       TaskInfoQO taskInfoQO//查询条件
    ) {
        DhPageInfo<TaskInfoVO> taskLists = taskInfoService.getAllTaskInfoByAccountId(pageIndex, pageSize, sortName, sortOrder, taskInfoQO);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), taskLists);
    }

    /**
     * 保存任务
     *
     * @param taskInfo
     * @return
     */
    @PostMapping("taskInfo/saveTaskInfo")
    public DataResponse saveTaskInfo(@RequestBody TaskInfo taskInfo) {
        return taskInfoService.saveTaskInfo(taskInfo) ? new DhResponse(ResponseCodeEnum.OK.getResponseCode()) : new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
    }

    /**
     * 根据主键查询任务信息
     *
     * @param taskId
     * @return
     */
    @GetMapping("taskInfo/getTaskInfoByTaskId")
    public DataResponse getTaskInfoByTaskId(@RequestParam("taskId") String taskId) {
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), taskInfoService.getTaskInfoByTaskId(taskId));
    }

    /***
     * @Description:用户端-任务列表—本日任务
     * @Param:selectDate 查询日期
     * @Param:pageIndex/pageSize 页码/每页显示数量
     * @Author: luojie
     * @Date: 2021/4/26 18:08
     */
    @GetMapping("/getTodayTask")
    public DataResponse getTodayTask(@RequestParam String selectDate,
                                    @RequestParam(value = "pageIndex", required = true) Integer pageIndex,
                                            @RequestParam(value = "pageSize", required = true) Integer pageSize
    ) throws ParseException {
        DhPageInfo<TodayTaskVO> todayTask = taskInfoService.getTodayTask(selectDate,pageIndex, pageSize);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), todayTask);
    }

    /**
     * 选中任务信息后获取任务分配人员
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param taskUserQO
     * @return
     */
    @PostMapping("taskUser/getTaskUserByTaskId")
    public DataResponse getTaskUserByTaskId(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                       @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                       @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                       @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                       TaskUserQO taskUserQO//查询条件
    ) {
        DhPageInfo<TaskUserVO> taskuserLists = taskUserService.getTaskUserByTaskId(pageIndex, pageSize, sortName, sortOrder, taskUserQO);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), taskuserLists);
    }

    /**
     * 保存分配
     * @param taskUsersQO
     * @return
     */
    @PostMapping("taskUser/saveAssignments")
    public DataResponse saveAssignments(@RequestBody TaskUsersQO taskUsersQO) {
        return taskUserService.saveTaskUser(taskUsersQO) ? new DhResponse(ResponseCodeEnum.OK.getResponseCode()) : new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
    }
}
